package com.jd.ty.web.filter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.constant.QueryConstants.Customer;
import com.jd.ty.dao.constant.QueryConstants.CustomerCompany;
import com.jd.ty.dao.constant.QueryConstants.Order;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.util.CharacterEncodingUtil;
import com.jd.ty.util.Filter;

public class CustomerOrderFilter extends Filter {
	private Item companyName = new Item(Order.CUSTOMER+'.'+Customer.CUST_COMPANY+'.'+CustomerCompany.CUST_COMPANY_NAME, null,
			QueryConstants.LIKE);
	private Item address = new Item(Order.CUSTOMER+'.'+Customer.CUST_COMPANY+'.'+CustomerCompany.ADDRESS, false, QueryConstants.GE);
	private Item firstTimeGE = new Item(Order.DEAL_DATE, null, QueryConstants.GE);
	private Item firstTimeLE = new Item(Order.DEAL_DATE, null, QueryConstants.LE);

	private Item expired = new Item(Order.DEAL_DATE, null, QueryConstants.LE);

	private boolean isExpired;

	public Date getFirstTimeGE() {
		return (Date) firstTimeGE.getValue();
	}

	public void setFirstTimeGE(Date firstTimeGE) {
		this.firstTimeGE.setValue(firstTimeGE);
	}

	public Date getFirstTimeLE() {
		return (Date) firstTimeLE.getValue();
	}

	public void setFirstTimeLE(Date firstTimeLE) {
		this.firstTimeLE.setValue(firstTimeLE);
	}

	public String getCompanyName() {
		return (String) companyName.getValue();
	}

	public void setCompanyName(String value) {
		companyName.setValue(CharacterEncodingUtil.iso2utf8(value));
	}

	public String getAddress() {
		return (String) address.getValue();
	}

	public void setAddress(String value) {
		address.setValue(CharacterEncodingUtil.iso2utf8(value));
	}

	public Boolean getExpired() {
		return isExpired;
	}

	public void setExpired(Boolean expired) {
		this.isExpired = expired;
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_YEAR, -365);
		this.expired.setValue(cal.getTime());
		if (expired.booleanValue()) {
			this.expired.setExp(QueryConstants.LE);
		} else {
			this.expired.setExp(QueryConstants.GE);
		}

	}

	public static void main(String[] args) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

		Calendar cal = Calendar.getInstance();

		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_YEAR, -365);
		cal.getTime();
		System.out.println("365 days after(or before) is " + format.format(cal.getTime()));

	}

	public String toHqlString() {
		if(isExpired){
			return super.toHqlString();
		}
		HqlCriterions hc = new HqlCriterions();
		hc.and(Order.DEAL_DATE, null, QueryConstants.EQ);
		hc.and(Order.DEAL_DATE, null, QueryConstants.EQ);
		hc.and(companyName.getName(), companyName.getValue(), companyName.getExp());
		hc.and(address.getName(),address.getValue(),address.getExp());
		return "("+hc.getHqlString()+") OR ("+super.toHqlString()+")";

	}

}
